﻿Imports System.IO

Public Class Settings

    Private theSettings As New ConfigOpt

    Public Sub New()
        theSettings.Initialize(Path.Combine(Application.StartupPath, "settings.config"))
    End Sub

    Public Sub save()
        theSettings.Store()
    End Sub

#Region "Settings"

    Public Property SelectedDevice() As String

        Get
            Dim str As String = theSettings.GetOption("SelectedDevice")

            If str = "" Then
                SelectedDevice = "NONE"
            Else
                SelectedDevice = str
            End If

        End Get

        Set(ByVal value As String)
            theSettings.SetOption("SelectedDevice", value)
        End Set

    End Property

    Public Property ShowDeviceIcon() As Boolean
        Get
            Dim str As String = theSettings.GetOption("ShowDeviceIcon")

            If str = "" Then
                ShowDeviceIcon = True
            Else
                ShowDeviceIcon = str
            End If
        End Get
        Set(ByVal value As Boolean)
            theSettings.SetOption("ShowDeviceIcon", value)
        End Set
    End Property

    Public Property FileManagementPanelSplitterDistance() As Integer
        Get
            Dim str As String = theSettings.GetOption("FileManagementPanelSplitterDistance")

            If str = "" Then
                FileManagementPanelSplitterDistance = 364
            Else
                FileManagementPanelSplitterDistance = str
            End If
        End Get
        Set(ByVal value As Integer)
            theSettings.SetOption("FileManagementPanelSplitterDistance", value)
        End Set
    End Property

#End Region

    Private Class ConfigOpt
        Private DSoptions As DataSet
        Private mConfigFileName As String

        Public ReadOnly Property ConfigFileName() As String
            Get
                Return mConfigFileName
            End Get
        End Property

        Public Sub Initialize(ByVal ConfigFile As String)
            mConfigFileName = ConfigFile
            DSoptions = New DataSet("ConfigOpt")
            If File.Exists(ConfigFile) Then
                Try
                    DSoptions.ReadXml(ConfigFile)
                Catch ex As Exception
                    Trace.WriteLine("Couldn't read settings " & ConfigFile & " - " & ex.Message)
                End Try
            Else
                Dim dt As New DataTable("ConfigValues")
                dt.Columns.Add("OptionName", System.Type.GetType("System.String"))
                dt.Columns.Add("OptionValue", System.Type.GetType("System.String"))
                dt.Columns("OptionName").ColumnMapping = MappingType.Attribute
                dt.Columns("OptionValue").ColumnMapping = MappingType.SimpleContent
                DSoptions.Tables.Add(dt)
            End If
        End Sub

        Public Sub Store()
            Store(mConfigFileName)
        End Sub

        Public Sub Store(ByVal ConfigFile As String)
            mConfigFileName = ConfigFile
            Try
                DSoptions.WriteXml(ConfigFile)
            Catch ex As Exception
                Trace.WriteLine("Couldn't save settings " & ConfigFile & " - " & ex.Message)
            End Try
        End Sub

        Public Function GetOption(ByVal OptionName As String) As String
            Dim dv As DataView = DSoptions.Tables("ConfigValues").DefaultView
            dv.RowFilter = "OptionName='" & OptionName & "'"
            If dv.Count > 0 Then
                Try
                    Return CStr(dv.Item(0).Item(1))
                Catch ex As Exception
                    Trace.WriteLine("Error reading setting '" & OptionName & "' - " & ex.Message)
                    Return ""
                End Try
            Else
                Return ""
            End If
        End Function

        Public Sub SetOption(ByVal OptionName As String, ByVal OptionValue As String)
            Dim dv As DataView = DSoptions.Tables("ConfigValues").DefaultView
            dv.RowFilter = "OptionName='" & OptionName & "'"
            If dv.Count > 0 Then
                dv.Item(0).Item(1) = OptionValue
            Else
                Dim dr As DataRow = DSoptions.Tables("ConfigValues").NewRow()
                dr("OptionName") = OptionName
                dr(1) = OptionValue
                DSoptions.Tables("ConfigValues").Rows.Add(dr)
            End If
        End Sub

    End Class

End Class